<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/2 0002
 * Time: 下午 8:55
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;

class Common extends Controller {
    protected $uid;
    protected $user_token;
    protected $user_token_time;

    public function _initialize()
    {
        $this->uid = session('uid');
        $this->user_token = session('user_token');
        $this->user_token_time = session('user_token_time');
        $request = Request::instance();
        $module = $request->module();
        $controller = $request->controller();
        $action = $request->action();
//        print_r($module.'/'.$controller.'/'.$action);die;
        if($controller != 'Login'){
            $this->isLogin();
        }
    }


    public function isLogin(){
        if(!$this->uid || !$this->user_token || !$this->user_token_time){
            exit('<script language="javascript">alert("请先登录");top.location.href="/login";</script>');
        }
        $userToken = md5('salt'.$this->user_token_time.$this->uid);
        if($userToken != $this->user_token){
            exit('<script language="javascript">alert("请先登录");top.location.href="/login";</script>');
        }
        $date = time() - $this->user_token_time;
        if($date > 30000){
            exit('<script language="javascript">alert("请先登录");top.location.href="/login";</script>');
        }
    }
}